package com.web;


import com.entity.Option;
import com.repository.OptionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@CrossOrigin(origins = "http://localhost:63343/")
//Restful API 注解
@RestController
// 请求地址映射
@RequestMapping("/api")
public class OptionController {
    @Autowired
    OptionRepository optionRepository;
    @GetMapping("/options")
//@GetMapping("/Options")
public ResponseEntity<List<Option>> getAllOptions(@RequestParam(required = false) String content) {
    try {
        List<Option> options = new ArrayList<Option>();

        if (content == null)
            optionRepository.findAll().forEach(options::add);
        else
            optionRepository.findByContent(content).forEach(options::add);

        if (options.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        return new ResponseEntity<>(options, HttpStatus.OK);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

    @GetMapping("/options/{id}")
    public ResponseEntity<Option> getOptionById(@PathVariable("id") long id) {
        Optional<Option> OptionData = optionRepository.findById(id);

        if (OptionData.isPresent()) {
            return new ResponseEntity<>(OptionData.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    @PostMapping("/options")
    public ResponseEntity<Option> createOption(@RequestBody Option option) {
        try {
            Option _option = optionRepository
                    .save(new Option(option.getContent(), option.getContent(), option.isAnswer()));
            return new ResponseEntity<>(_option, HttpStatus.CREATED);
        } catch (Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    @PutMapping("/options/{id}")
    public ResponseEntity<Option> updateOption(@PathVariable("id") long id, @RequestBody Option option) {
        Optional<Option> optionData = optionRepository.findById(id);

        if (optionData.isPresent()) {
            Option _option = optionData.get();
            _option.setContent(option.getContent());
            _option.setLabel(option.getLabel());
            _option.setAnswer(option.isAnswer());
            return new ResponseEntity<>(optionRepository.save(_option), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/options/{id}")
    public ResponseEntity<HttpStatus> deleteOption(@PathVariable("id") long id) {
        try {
            optionRepository.deleteById(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/options")
    public ResponseEntity<HttpStatus> deleteAllOptions() {
        try {
            optionRepository.deleteAll();
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    @GetMapping("/options/IsAnswer")
    public ResponseEntity<List<Option>> findByIsAnswer() {
        try {
            List<Option> options = optionRepository.findByIsAnswer(true);

            if (options.isEmpty()) {
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }
            return new ResponseEntity<>(options, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }



}
